<?php
/**
 * @copyright Arthur Beer
 * @package reflection
 *
 * @copyright TinyWEB CMS
 * @link http://tinywebcms.ru
 * @link http://cms.vingrad.ru
 * @version SVN: $Id$ 
 */
 
/**
 * AutoloadCollectClasses
 */
class AutoloadCollectClasses extends Object 
{
    private $path = HOME_PATH;
    private $ext = '.php';
    private $exclude = array('forum');
    
    /**
     * Autoload config
     *
     * @var SimpleXMLElement
     */
    private $autoload = null;
    
    /**
     * Конструктор класса
     * 
     */
    public function __construct($args)
    {
        $this->autoload = simplexml_load_string("<?xml version=\"1.0\" encoding=\"utf-8\"?><autoload time=\"" . time() . "\"/>");
        $this->autoload->addChild('instances');
    }
    
    public function exec()
    {
        $this->scan($this->path);
    }
    
    protected function scan($directory)
    {
        /* @var $iterator SplFileInfo */
        $iterator;
        
        foreach (new RecursiveDirectoryIterator($directory) as $iterator) {
            if(!$iterator->isReadable()) {
                continue;
            }
            
            if($iterator->isDir() && !in_array($iterator->getFilename(), $this->exclude)) {
                $this->scan($iterator->getPathname());
            } else if($iterator->isFile() && mb_strpos(strrev($iterator->getFilename()), strrev($this->ext)) === 0) {
                echo "scan " . str_replace(array($this->path, '\\'), array('', '/'), $iterator->getPathname()) . "\n";
                $this->parse($iterator);
            }
        }
    }
    
    protected function parse(SplFileInfo $file)
    {
        if(preg_match_all("/(abstract\s+)?(?<type>class|interface)\s+(?<classname>[a-z_][a-z0-9_]+)((\s+extends\s+[a-z_][a-z0-9_]+)?(\s+implements\s+[a-z_][a-z0-9_,\s]+)?)?\s+\{/uis", file_get_contents($file->getPathname()), $matches)) {
        	foreach ($matches['classname'] as $key => $class) {
                /* @var $instance SimpleXMLElement */
                $instance = $this->autoload->instances->addChild($class);
                $instance->addAttribute('path', str_replace(array($this->path, '\\'), array('', '/'), $file->getPathname()));
                $instance->addAttribute('type', $matches['type'][$key]);
            }
        }
    }
    
    public function __destruct()
    {
        $this->autoload->asXML(FRAMEWORK_CORE_PATH . '/autoload/autoload.conf');
    }
}

/**
 * Tab: 4 spaces
 * Encoding: utf-8
 */
?>